package edu.umn.se.trap.logicChecker;

import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import edu.umn.se.trap.Day;
import edu.umn.se.trap.Form;
import edu.umn.se.trap.Grant;
import edu.umn.se.trap.Incidental;
import edu.umn.se.trap.Lodging;
import edu.umn.se.trap.Meal;
import edu.umn.se.trap.Transportation;
import databases.GrantDB;
import databases.KeyNotFoundException;
import databases.GrantDB.GRANT_FIELDS;
import edu.umn.se.trap.exceptions.TrapException;

public class CheckLogicRentalAgency implements Observer {

    @Override
    /**
     * Update for checking logic for rental agency
     */
    public void update(Observable o, Object arg) 
    {
        LogicChecker l1 = (LogicChecker) o;
        Form form = (Form) arg;
        GrantDB grantDB = new GrantDB();
        
        Vector<Grant> grants = form.getGrants();
        int count = 0;
        for (int i = 0; i < grants.size(); i++)
        {
            Grant grant = grants.elementAt(i);
            List<Object> grantInfo;
            
            try 
            {
                grantInfo = grantDB.getGrantInfo(String.valueOf(grant.getAccountNumber()));
                int index = GRANT_FIELDS.FUNDING_ORGANIZATION.ordinal();
                String fundingOrg = (String) grantInfo.get(index);
                if (fundingOrg.equalsIgnoreCase("DOD"))
                {
                    count++;
                }
            } 
            catch (KeyNotFoundException e) 
            {
                l1.setException(new TrapException("Key Invalid - ForeignGov"));
            }
        }
        
        // if not all of the grants are DOD, then rental agency National Travel must be used
        if (count != grants.size())
        {
            Vector<Transportation> transports = form.getTransportation();            
            for (int i = 0; i < transports.size(); i++)
            {
                Transportation transport = transports.get(i);
                boolean rental = transport.getRental();
                String carrier = transport.getCarrier();
                
                boolean inUSA = false;
                Vector<Day> days = form.getDays();
                for (int j = 0; j < days.size(); j++)
                {
                    Day today = days.get(j);
                    //System.out.print( today.getDate() + " " + transport.getDate() + "\n");
                    if (today.getDate().equals(transport.getDate())) 
                    {
                        Meal breakfast = today.getBreakfast();
                        Meal lunch = today.getLunch();
                        Meal dinner = today.getDinner();
                        Incidental incidental = today.getIncidentals();
                        Lodging lodging = today.getLodging();
                        if ((!breakfast.getCountry().equals("") && (breakfast.getCountry().equalsIgnoreCase("USA") || breakfast.getCountry().equalsIgnoreCase("United States"))) || 
                                (!lunch.getCountry().equals("") && (lunch.getCountry().equalsIgnoreCase("USA") || lunch.getCountry().equalsIgnoreCase("United States"))) ||
                                (!dinner.getCountry().equals("") && (dinner.getCountry().equalsIgnoreCase("USA") || dinner.getCountry().equalsIgnoreCase("United States"))) ||
                                (!lodging.getCountry().equals("") && (lodging.getCountry().equalsIgnoreCase("USA") || lodging.getCountry().equalsIgnoreCase("United States"))) ||
                                (!incidental.getCountry().equals("") && (incidental.getCountry().equalsIgnoreCase("USA") || lodging.getCountry().equalsIgnoreCase("United States"))))
                        {
                            inUSA = true;
                        }
                    }
                }
                
                if(inUSA && rental && !carrier.equalsIgnoreCase("National travel"))
                {
                    l1.setException(new TrapException("Domestic rentals must be with National Travel"));
                }   
             }
        }
    }
}